home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Pointer to int
- Date: Thu, 14 Mar 96 18:57:10 GMT
- Organization: none
- Distribution: world
- Message-ID: <826829830snz@genesis.demon.co.uk>
- References: <4i76vf$58v@dunlop.cs.strath.ac.uk>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4i76vf$58v@dunlop.cs.strath.ac.uk>
- abrown@cs.strath.ac.uk "Andrew Brown" writes:
-
- >I have a function that takes as a parameter a pointer to an int. Is it
- >possible to pass the address of a constant directly to the function?
-
- No. Pointers need to point to something that in some sense has an address,
- in this case an object in memory. A value doesn't exist at a specific memory
- address and you can't generate a pointer to one.
-
- >eg.
- >
- >If the function is defined as ...
- >
- > int function( int * ptr );
- >
- >could I call it with something like ...
- >
- > x = function( &1000 );
- >
- >where 1000 is the constant to which I want ptr to reference.
-
- You can't do this. However you could do something like:
-
- {
- static const int value = 1000;
-
- x = function( &value );
- }
-
- The static doesn't change the effect of the program in this case but it
- may help the compiler to generate better code. Clearly function() must
- never write through the pointer that is its argument and should be declared
- as:
-
- int function( const int * ptr );
-
- Given this the question is why you pass a pointer to the value rather than
- the value itself?
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-